import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) {
        try {
            // 创建一个服务器,打开端口号
            ServerSocket serverSocket = new ServerSocket(8888);

            System.out.println("创建 TCP 服务器，等待客户段连接");
            // 等待客户机连接
            Socket socket = serverSocket.accept();
            System.out.println("客户端连接成功");

            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();

            byte[] input = new byte[1024];
            byte[] output = {'H','e','l','l','o',',','w','o','r','l','d','!'};

            outputStream.write(output);

            inputStream.close();
            outputStream.close();
            socket.close();
            serverSocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
